package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import myfamily.model.Contact;
import myfamily.model.GenericPerson;
import myfamily.model.Name;
import myfamily.model.RegisteredUser;

import org.expressme.openid.Authentication;

import sun.misc.Sort;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyTree extends ActionSupport{
	private List<String> operations;
	private List<String> familymembers;
	private String selectedOperation;
	private String selectedMember;
	private String memberName;
	private String memberGender;
	private String memberEmailId;
	private String memberDOB;
	private String submit;
	private String treeString;
	private String user;
	private String authId;
	private String associatedSpouse;
	private GenericPerson selectedPerson;

	public String execute(){
		try{
			
			System.out.println("In Modify Tree");
			Map session = ActionContext.getContext().getSession();
			Authentication auth = new Authentication();
			auth = (Authentication)session.get("authentication");
			setAuthId(auth.getIdentity().split("=")[1]);
			RegisteredUser ru = RegisteredUser.login(getAuthId());
			
			operations = new ArrayList<String>();
			operations.add("Add Child");
			operations.add("Add Parents");
			operations.add("Add Partner");
			operations.add("Add Sibling");
			operations.add("Delete Member");
			operations.add("Edit Details");
			familymembers = new ArrayList<String>();
			if(submit==null){
				System.out.println(getSubmit());
				familymembers.clear();
				ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
				for(GenericPerson g: fm){
					familymembers.add(g.getPersonName().toString());
					
				}
				
				setTreeString(ru.printTree().toString());
				return "showModifyTreeOptions";	

			}else{
				
				ArrayList<GenericPerson> agp = ru.identifyExtremeAncestors();
				for(GenericPerson g: agp){
					if(g.getPersonName().toString().equals(getSelectedMember())){
						setSelectedPerson(g);
						break;
					}
					
				}
				if(submit.equals("OK")){
					
					System.out.println(getSubmit());
					System.out.println("getSelectedMember:"+getSelectedMember());
					System.out.println("getSelectedOperation:"+getSelectedOperation());
					System.out.println("memberName:"+getMemberName());
					System.out.println("memberEmailId:"+getMemberEmailId());
					System.out.println("memberGender:"+getMemberGender());
					
					
					if(selectedOperation.equals("Add Parents")){
						
						GenericPerson gp1;
						if(getMemberName().split(" ").length == 1)
							gp1 = ru.createUnregisteredUser(new Name(getMemberName(), "", getSelectedPerson().getPersonName().getLastName()),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
						else
							gp1 = ru.createUnregisteredUser(new Name(getMemberName().split(" ")[0], "", getMemberName().split(" ")[1]),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));

						//gp2 = ru.createDummyProfile();
						//getSelectedPerson().addParents(gp1,gp2);
						getSelectedPerson().addParent(gp1);
						/* update the familymembers list*/
						familymembers.clear();
						ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
						for(GenericPerson g: fm){
							familymembers.add(g.getPersonName().toString());
							
						}
						
						setTreeString(ru.printTree().toString());
						return "showModifiedTree";

					}
					if(selectedOperation.equals("Add Partner")){
						GenericPerson gp;
						if(getMemberName().split(" ").length == 1)
							gp = ru.createUnregisteredUser(new Name(getMemberName(), "", getSelectedPerson().getPersonName().getLastName()),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
						else
							gp = ru.createUnregisteredUser(new Name(getMemberName().split(" ")[0], "", getMemberName().split(" ")[1]),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));

						getSelectedPerson().addSpouse(gp);

						/* update the familymembers list*/
						familymembers.clear();
						ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
						for(GenericPerson g: fm){
							familymembers.add(g.getPersonName().toString());
							
						}
						
						setTreeString(ru.printTree().toString());
						return "showModifiedTree";
					}
					if(selectedOperation.equals("Add Child")){
						session.put("selectedPerson", getSelectedPerson());
						return "redirectAddChild";
						
					}
					if(selectedOperation.equals("Add Sibling")){
						GenericPerson gp;
						if(getMemberName().split(" ").length == 1)
							gp = ru.createUnregisteredUser(new Name(getMemberName(), "", getSelectedPerson().getPersonName().getLastName()),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));
						else
							gp = ru.createUnregisteredUser(new Name(getMemberName().split(" ")[0], "", getMemberName().split(" ")[1]),getMemberGender(),"","",null,null,new Contact("",getMemberEmailId()));

						getSelectedPerson().addSibling(gp,ru);
						
						/* update the familymembers list*/
						familymembers.clear();
						ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
						for(GenericPerson g: fm){
							familymembers.add(g.getPersonName().toString());
							
						}
						
						setTreeString(ru.printTree().toString());
						return "showModifiedTree";
					}
					if(selectedOperation.equals("Edit Details")){
						session.put("selectedPerson", getSelectedPerson());
						return "redirectModifyProfile";
					}
					
					if(selectedOperation.equals("Delete Member")){
						
						ArrayList<GenericPerson> gp = ru.identifyExtremeAncestors();
						Boolean result;
						for(GenericPerson g: gp){
							
							if(g.getPersonName().toString().equals(selectedMember)){
								
								result = g.deleteMe();
								if(result!=true){
									addActionError("The Selected Member could not be deleted");
								}
							}
						}

						/* update the familymembers list*/
						familymembers.clear();
						ArrayList<GenericPerson> fm = ru.identifyExtremeAncestors();
						for(GenericPerson g: fm){
							familymembers.add(g.getPersonName().toString());
							
						}
						
						setTreeString(ru.printTree().toString());
						return "showModifiedTree";
					}

	
				}
				return "showModifyTreeOptions";	

			}
		}catch(Exception e){
	e.printStackTrace();
		}	

		
		
		
		
		
		
		
		return "showLogin";
	}

	public List<String> getOperations() {
		return operations;
	}

	public void setOperations(List<String> operations) {
		this.operations = operations;
	}

	public List<String> getFamilymembers() {
		return familymembers;
	}

	public void setFamilymembers(List<String> familymembers) {
		this.familymembers = familymembers;
	}

	public String getSelectedOperation() {
		return selectedOperation;
	}

	public void setSelectedOperation(String selectedOperation) {
		this.selectedOperation = selectedOperation;
	}

	public String getSelectedMember() {
		return selectedMember;
	}

	public void setSelectedMember(String selectedMember) {
		this.selectedMember = selectedMember;
	}

	public String getMemberName() {
		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public String getMemberGender() {
		return memberGender;
	}

	public void setMemberGender(String memberGender) {
		this.memberGender = memberGender;
	}

	public String getMemberEmailId() {
		return memberEmailId;
	}

	public void setMemberEmailId(String memberEmailId) {
		this.memberEmailId = memberEmailId;
	}

	public String getMemberDOB() {
		return memberDOB;
	}

	public void setMemberDOB(String memberDOB) {
		this.memberDOB = memberDOB;
	}

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public String getTreeString() {
		return treeString;
	}

	public void setTreeString(String treeString) {
		this.treeString = treeString;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getAuthId() {
		return authId;
	}

	public void setAuthId(String authId) {
		this.authId = authId;
	}

	public String getAssociatedSpouse() {
		return associatedSpouse;
	}

	public void setAssociatedSpouse(String associatedSpouse) {
		this.associatedSpouse = associatedSpouse;
	}

	public GenericPerson getSelectedPerson() {
		return selectedPerson;
	}

	public void setSelectedPerson(GenericPerson selectedPerson) {
		this.selectedPerson = selectedPerson;
	}

}
